import type { RouteRecordRaw } from 'vue-router'

/** 基础路由 */
const baseRoutes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/home.vue'),
    meta: { title: '首页', icon: 'user' }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login.vue'),
    meta: { title: '登录', icon: 'user' }
  },
]

/**
 * 从路由数组中获取所有路由的完整路径
 * @param {*} routes 
 * @param {*} rootPath 父级路径
 * @returns 路径数组
 */
function filterPath(routes: any, rootPath = '') {

  return routes.reduce((pre: any, currentItem: any, index: number) => {

    if (currentItem.path.startsWith('/')) {
      pre.push(currentItem.path)
    } else {
      pre.push(rootPath + currentItem.path)
    }

    if (currentItem.children) {
      pre = pre.concat(filterPath(currentItem.children, rootPath + currentItem.path + '/'))
    }

    return pre

  }, [])

}

/** 路由路径白名单 */
export const whitePath = filterPath(baseRoutes)

export default baseRoutes